
import { VuexModule, Module, Action, Mutation, getModule, MutationAction } from 'vuex-module-decorators';
import store from '@/store';
import servers from '@/servers/index'
import { GetSocketModule } from "@/store/modules/GetSocketData";

// interface
export interface AuxiliaryMonitorState {
  activeRouter: number;
  stationName: string;
  thresholdvalueBtn: number
}

// 辅助监控
@Module({ dynamic: true, store, name: 'AuxiliaryMonitor', namespaced: true })
class AuxiliaryMonitor extends VuexModule implements AuxiliaryMonitorState {
  // state
  public activeRouter = 1
  public stationName = ''//下拉框选中的变电站标题
  public stationId = ''//下拉框选中的变电站标题对应的id
  public thresholdvalueBtn: number = 0;// 0：主设备 1：辅控 2：在线监测
  public ThresholdItemList: any = {
    tableData: [],
    pages: {
      pageSize: 10,
      page: 1,
      totalSize: 10,
    }
  }

  public treeList1 = {}
  public treeList2 = {}
  public treeList3 = {}

  public treeList0=''
  //  getter （直接获取）
  get getterActiveRouter() {
    return this.activeRouter
  }

  get getterthresholdvalueBtn() {
    return this.thresholdvalueBtn
  }

  get getterstationId() {
    return this.stationId
  }

  get getterstationName() {
    return this.stationName
  }

  // 获取某个变电站-(消防、给排水、照明控制、安全防范、环境监控系统)的点位数
  get getxtInfo() {
    let xtInfo = GetSocketModule.getterAllAuxiliaryByStationId || {}
    var id = this.stationId
    // console.log('id:',id);
    // console.log(999,xtInfo)
    var xtList = xtInfo[id] || []
    let xtobj = {
      '01': [],
      '06': [],
      '05': [],
      '02': [],
      '04': [],
    }
    let objKeys = Object.keys(xtobj)
    if (xtList.length > 0) {
      xtList.map(k => {
        if (objKeys.indexOf(k.xtValue) >= 0) {
          xtobj[k.xtValue] = xtobj[k.xtValue] || []
          xtobj[k.xtValue].push(k)
        }
      })
    }
    // console.log('AuxiliaryMonitorModule:',xtobj);
    return xtobj
  }

  // Action
  @Action({ commit: 'setActiveRouter' })
  getActiveRouter(activeRouter: number) {
    return activeRouter;
  }
  @Action({ commit: 'setthresholdvalueBtn' })
  getthresholdvalueBtn(thresholdvalueBtn: number) {
    return thresholdvalueBtn;
  }
  @Action({ commit: 'settreeList1' })
  gettreeList1(treeList1: any) {
    return treeList1;
  }
  @Action({ commit: 'settreeList2' })
  gettreeList2(treeList2: any) {
    return treeList2;
  }
  @Action({ commit: 'settreeList3' })
  gettreeList3(treeList3: any) {
    return treeList3;
  }

  @Action({ commit: 'settreeList0' })
  gettreeList0(treeList0: any) {
    return treeList0;
  }
  // Mutation
  @Mutation
  setActiveRouter(activeRouter: number) {
    this.activeRouter = activeRouter;
  }
  @Mutation
  setStationName(item: any) {
    if (item.name) {
      this.stationName = item.name;
      this.stationId = item.code;
    }
  }
  @Mutation
  setthresholdvalueBtn(thresholdvalueBtn: number) {
    this.thresholdvalueBtn = thresholdvalueBtn;
    this.ThresholdItemList.pages.page = 1
  }
  @Mutation
  settreeList1(treeList1: any) {
    this.treeList1 = treeList1;
  }
  @Mutation
  settreeList2(treeList2: any) {
    this.treeList2 = treeList2;
  }
  @Mutation
  settreeList3(treeList3: any) {
    this.treeList3 = treeList3;
  }
  @Mutation
  settreeList0(treeList0: any) {
    this.treeList0 = treeList0;
    // console.log(this.treeList0);
    
  }
  //阈值设置-列表查询
  @MutationAction({ mutate: ['ThresholdItemList'] })
  async queryThresholdItem(params) {
    var list: any = await (servers as any)
      .queryThresholdItem({ data: params, method: 'post' }).then((res: any) => {
        // console.log(res.data);
        var result = {
          tableData: res.data.records,
          pages: {
            pageSize: res.data.size,
            page: res.data.current,
            totalSize: res.data.total,
          }
        }
        return result
      })
    return { ThresholdItemList: list }
  }
}

export const AuxiliaryMonitorModule = getModule(AuxiliaryMonitor);
